﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

namespace Vehicle_Maintenance
{
    class Vehicle
    {
        public Vehicle() { }
        public string VIN        = string.Empty;
        public string Owner_ID   = string.Empty;
        public string Year       = string.Empty;
        public string Make       = string.Empty;
        public string Model      = string.Empty;
        public string Varient    = string.Empty;
        public string T_Gal      = string.Empty;
        public string T_Spent    = string.Empty;
        public string Orig_Odom  = string.Empty;
        public string Curr_Odom  = string.Empty;
        public string Name       = string.Empty;
        public string Reg_Num    = string.Empty;
        public string Tire_Size  = string.Empty;
        public string Eng_Size   = string.Empty;
        public string Tabs_Due   = string.Empty;
        public string Insur      = string.Empty;

        public bool Load()
        {
            bool b_isLoaded = false;
            if ( !string.IsNullOrWhiteSpace( this.VIN ) )
            {
                DataTable dt_car = new DataTable();
                using ( SqlConnection scConnect = new SqlConnection( Properties.Settings.Default.Auto_Log ) )
                {
                    scConnect.Open();
                    SqlCommand scCommand  = new SqlCommand();
                    scCommand.Connection  = scConnect;
                    scCommand.CommandType = CommandType.Text;
                    scCommand.CommandText = "SELECT * FROM dbo.Vehicles WHERE VIN = '" + this.VIN + "'";
                    dt_car.Load( scCommand.ExecuteReader() );
                    scConnect.Close();
                }
                using ( DataTableReader dtr = new DataTableReader( dt_car ) )
                {
                    if ( dtr.HasRows )
                    {
                        dtr.Read();
                        Owner_ID    = dtr[ "Owner_ID" ].ToString();
                        Year        = dtr[ "Year" ].ToString();
                        Make        = dtr[ "Make" ].ToString();
                        Model       = dtr[ "Model" ].ToString();
                        Varient     = dtr[ "Model_Vari" ].ToString();
                        T_Gal       = dtr[ "T_Gal" ].ToString();
                        T_Spent     = dtr[ "T_Spent" ].ToString();
                        Orig_Odom   = dtr[ "Orig_Odom" ].ToString();
                        Curr_Odom   = dtr[ "Curr_Odom" ].ToString();
                        Name        = dtr[ "Name" ].ToString();
                        Reg_Num     = dtr[ "Reg_Num" ].ToString();
                        Tire_Size   = dtr[ "Tire_Size" ].ToString();
                        Eng_Size    = dtr[ "Eng_Size" ].ToString();
                        Tabs_Due    = dtr[ "Tabs_Due" ].ToString();
                        Insur       = dtr[ "Insur" ].ToString();
                        b_isLoaded  = true;
                    }
                }
            }
            return b_isLoaded;
        }
        public bool LoadByVIN( string vin )
        {
            VIN = vin;
            return Load();
        }

        public void New()
        {
            using (SqlConnection scConnect = new SqlConnection(Properties.Settings.Default.Auto_Log))
            {
                scConnect.Open();
                SqlCommand scCommand = new SqlCommand();
                scCommand.Connection = scConnect;
                scCommand.CommandType = CommandType.StoredProcedure;
                scCommand.CommandText = "vehicles_insert";
                scCommand.Parameters.AddWithValue("@VIN", this.VIN);
                scCommand.Parameters.AddWithValue("@Owner_ID", this.Owner_ID);
                scCommand.Parameters.AddWithValue("@Year", this.Year);
                scCommand.Parameters.AddWithValue("@Make", this.Make);
                scCommand.Parameters.AddWithValue("@Model", this.Model);
                scCommand.Parameters.AddWithValue("@T_Gal", this.T_Gal);
                scCommand.Parameters.AddWithValue("@t_Spent", this.T_Spent);
                scCommand.Parameters.AddWithValue("@Orig_Odom", this.Orig_Odom);
                scCommand.Parameters.AddWithValue("@Curr_Odom", this.Curr_Odom);
                scCommand.Parameters.AddWithValue("@Name", this.Name);
                scCommand.Parameters.AddWithValue("@Reg_Num", this.Reg_Num);
                scCommand.Parameters.AddWithValue("@Model_Vari", this.Varient);
                scCommand.Parameters.AddWithValue("@Tire_Size", this.Tire_Size);
                scCommand.Parameters.AddWithValue("@Eng_Size", this.Eng_Size);
                scCommand.Parameters.AddWithValue("@Tabs_Due", DateTime.Now.ToShortDateString());
                scCommand.Parameters.AddWithValue("@Insur", this.Insur);
                //scCommand.Parameters["@VIN"].Direction = ParameterDirection.Output;
                //scCommand.Parameters["@VIN"].Size = 16;
                scCommand.ExecuteNonQuery();
                //this.VIN = scCommand.Parameters["@VIN"].Value.ToString();
                scConnect.Close();
            }
        }

        public void Update()
        {
            using ( SqlConnection scConnect = new SqlConnection( Properties.Settings.Default.Auto_Log ) )
            {
                scConnect.Open();
                SqlCommand scCommand = new SqlCommand();
                scCommand.Connection = scConnect;
                scCommand.CommandType = CommandType.StoredProcedure;
                scCommand.CommandText = "update_vehicle";
                scCommand.Parameters.AddWithValue( "@VIN", this.VIN );
                scCommand.Parameters.AddWithValue( "@Year", this.Year );
                scCommand.Parameters.AddWithValue( "@Make", this.Make );
                scCommand.Parameters.AddWithValue( "@Model", this.Model );
                scCommand.Parameters.AddWithValue( "@Name", this.Name );
                scCommand.Parameters.AddWithValue( "@Reg_Num", this.Reg_Num );
                scCommand.Parameters.AddWithValue( "@Model_Vari", this.Varient );
                scCommand.Parameters.AddWithValue( "@Tire_Size", this.Tire_Size );
                scCommand.Parameters.AddWithValue( "@Eng_Size", this.Eng_Size );
                scCommand.Parameters.AddWithValue( "@Tabs_Due", this.Tabs_Due );
                scCommand.Parameters.AddWithValue( "@Insur", this.Insur );
                scCommand.ExecuteNonQuery();
                scConnect.Close();
            }
        }
    }
}
